package exam.model.dao;

import java.util.HashMap;
import java.util.List;

import exam.common.util.StringUtils;
import exam.model.entity.TAnswerEntity;
import exam.system.dao.AbstractDAO;

public class TAnswerDAO extends AbstractDAO<TAnswerEntity>{
	
	public TAnswerDAO() {
		super(TAnswerEntity.class);
	}
	
	public List<TAnswerEntity> selectByQuestionId(long questionId) {
		String query = "select a from TAnswerEntity a inner join a.questionEntity where a.questionId="+questionId;
		return super.selectList(query);
	}
	
	/**
	 * 
	 * @param listQuestionId
	 * @return
	 */
	public List<TAnswerEntity> select002(List<Long> listQuestionId) {
		String query = "select a from TAnswerEntity a where a.questionId in " 
				+ StringUtils.replaceStartAndEndForList(listQuestionId, "(", ")");
		return super.selectList(query);
	}
	
	public TAnswerEntity selectByPk(long id) {
		return super.selectByPk(id);
	}
	
	/**
	 * insertAnswer: (update: 21/5/2012)
	 * @param tAnswerEntity
	 * @return
	 */
	public int insertAnswer(TAnswerEntity tAnswerEntity) {
		return super.insert(tAnswerEntity);
	}
	
	/**
	 * updateAnswer: (update: 21/5/2012)
	 * @param tAnswerEntity
	 * @return
	 */
	public int updateAnswer(TAnswerEntity tAnswerEntity) {
		String query = "update TAnswerEntity a set a.answerContent=:answerContent, " +
				"a.answerCorrect=:answerCorrect where a.answerId=:answerId";
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("answerContent", tAnswerEntity.getAnswerContent());
		params.put("answerCorrect", tAnswerEntity.getAnswerCorrect());
		params.put("answerId", tAnswerEntity.getAnswerId());
		return super.updateByQueryParams(query, params);
	}
}
